package _11_整理题目._6_模拟设计;

import org.junit.Test;

import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;

/**
 * https://leetcode.cn/problems/design-a-food-rating-system/
 *
 * foods, cuisines, ratings
 *
 */
public class _22_食物评分系统 {

    // 定义成一个 Pair，方便后续排序，查找
    class Pair {
        String food;
        String cuisine;
        int rating;

        public Pair(String food, String cuisine, int rating) {
            this.food = food;
            this.cuisine = cuisine;
            this.rating = rating;
        }
    }

    // 一个 food to Pair 的 map
    Map<String, Pair> foodToPair = new HashMap<>();
    // 每个 cuisine to Pair 的 treeMap，自定义排序
    Map<String, TreeSet<Pair>> cuisineToPair = new HashMap<>();


    public _22_食物评分系统(String[] foods, String[] cuisines, int[] ratings) {
        int n = foods.length;
        for (int i = 0; i < n; i++) {
            // 构建 food to Pair 的 map
            Pair pair = new Pair(foods[i], cuisines[i], ratings[i]);
            foodToPair.put(foods[i], pair);
            // 根据不同 烹饪方式 分类存储到 treemap
            if (cuisineToPair.containsKey(cuisines[i])) {
                cuisineToPair.get(cuisines[i]).add(pair);
            } else {
                TreeSet<Pair> treeSet = new TreeSet<>(
                        (o1, o2) -> {
                            if (o2.rating != o1.rating) {
                                return o2.rating - o1.rating;
                            } else {
                                return o1.food.compareTo(o2.food);
                            }
                        }
                );
                treeSet.add(pair);
                cuisineToPair.put(cuisines[i], treeSet);
            }
        }
    }

    public void changeRating(String food, int newRating) {
        Pair pair = foodToPair.get(food);
        cuisineToPair.get(pair.cuisine).remove(pair);
        pair.rating = newRating;
        foodToPair.put(food, pair);
        cuisineToPair.get(pair.cuisine).add(pair);
    }

    public String highestRated(String cuisine) {
        return cuisineToPair.get(cuisine).first().food;
    }

    public static void main(String[] args) {
        String[] foods = new String[] {"kimchi", "miso", "sushi", "moussaka", "ramen", "bulgogi"};
        String[] cuisines = new String[] {"korean", "japanese", "japanese", "greek", "japanese", "korean"};
        int[] ratings = new int[] {9,12,8,15,14,7};
        _22_食物评分系统 foodRatings = new _22_食物评分系统(foods, cuisines, ratings);

        System.out.println(foodRatings.highestRated("korean"));
        System.out.println(foodRatings.highestRated("japanese"));; // 返回 "ramen"
        // "ramen" 是分数最高的日式料理，评分为 14 。
        foodRatings.changeRating("sushi", 16); // "sushi" 现在评分变更为 16 。
        System.out.println(foodRatings.highestRated("japanese"));; // 返回 "sushi"
        // "sushi" 是分数最高的日式料理，评分为 16 。
        foodRatings.changeRating("ramen", 16); // "ramen" 现在评分变更为 16 。
        System.out.println(foodRatings.highestRated("japanese"));; // 返回 "ramen"
        // "sushi" 和 "ramen" 的评分都是 16 。
        // 但是，"ramen" 的字典序比 "sushi" 更小。
    }

}
